package com.hbgjq.security.controller;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @PreAuthorize("hasPermission('authority','USER')")
 *
 * @author :Gjq
 * @date :2021/1/6
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @PreAuthorize("hasPermission('authority','USER')")
    @GetMapping("/1")
    public String t1() {
        SecurityContext context = SecurityContextHolder.getContext();
        return "ok 1 user";
    }

    @PreAuthorize("hasPermission('authority','ADMIN')")
    @GetMapping("/2")
    public String t2() {
        return "ok 2 admin";
    }


    /**
     * hasPermission
     */
    @PreAuthorize("hasPermission('authority','USER')")
    @GetMapping("/3")
    public String t3() {
        return "ok 3 admin or user";
    }

    /**
     * 禁用所有 permitAll() 允许所有
     */
    @PreAuthorize("denyAll()")
    @GetMapping("/4")
    public String t4() {
        return "ok 4 admin or user";
    }

    /**
     * haseRole 权限必须以ROLE_ 开头
     */
    @PreAuthorize("hasRole('USER')")
    @GetMapping("/5")
    public String t5() {
        return "ok 5 admin or user";
    }

    /**
     * spring el 表达式
     */
    @PreAuthorize("#s == '1'")
    @GetMapping("/6")
    public String t6(String s) {
        return "ok 6 ";
    }
}
